package geppetto.phraseHMM.phraseExtraction;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class PhraseExtractionLaTeXOutput {
	// output stream writer
	private PrintStream _out;

	public PhraseExtractionLaTeXOutput(String filename) {
		try {
			_out = new PrintStream(new FileOutputStream(filename), true, "UTF-8");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public void writeBegin() {
		_out.print("\\documentclass{article}[10pt]" + "\n");
		_out.print("\\usepackage{longtable}" + "\n");
		_out.print("\\usepackage{rotating}" + "\n");
		_out.print("\\usepackage{xcolor}"  + "\n");
		_out.print("\\usepackage[utf8]{inputenc}"  + "\n");
		_out.print("\\usepackage[encapsulated]{CJK}"  + "\n");
		_out.print("\\usepackage{ucs}"  + "\n");
		_out.print("\\begin{document}"  + "\n");
		_out.print("\\begin{CJK*}{UTF8}{gbsn}"  + "\n");

		_out.print("\n");
	}

	public void writePageTitle(int i, String source, String target) {
		_out.print("\\centering" + "\n");
		_out.print("{\\large \\textbf{Sentence Number " + i + "}}" + "\n");
		_out.print("\\\\[1cm]" + "\n");
		_out.print("\\raggedright" + "\n");
		_out.print("{\\normalsize \\textcolor{red}{\\textbf{Source: }} " + source + "}" + "\n");
		_out.print("\\\\[0.1cm]" + "\n");
		_out.print("\\raggedright" + "\n");
		_out.print("{\\normalsize \\textcolor{red}{\\textbf{Target: }} " + target + "}" + "\n");
		_out.print("\\\\[1cm]" + "\n");
	}

	public void writePhrasePairTableBegin() {
		_out.print("{\\small" + "\n");
		_out.print("\\begin{longtable}{| p{7cm} | p{7cm} | c }" + "\n");
		// header for the first page of the table is empty
		_out.print("\\endfirsthead" + "\n");
		// header for the remaining page(s) of the table
		_out.print("\\multicolumn{2}{c}{Continued\\ldots}" + "\n");
		_out.print("\\endhead" + "\n");
		// footer for all pages except the last page of the table
		_out.print("\\multicolumn{2}{c}{Continue on next page\\ldots}" + "\n");
		_out.print("\\endfoot" + "\n");
		// footer for the last page of the table
		_out.print("\\endlastfoot" + "\n");
		// write first row
		_out.print("\\hline" + "\n");
		_out.print("\\textbf{Source Phrase} & \\textbf{Target Phrase}\\\\" + "\n");
		_out.print("\\hline" + "\n");
	}

	public void writePhrasePairTableEntry(String sourcePhrase, String targetPhrase, double score) {
		_out.print(filterOutputString(sourcePhrase) + " & " + filterOutputString(targetPhrase)+ " & " + score +   "\\\\" + "\n");
		_out.print("\\hline" + "\n");
	}

	public void writePhrasePairTableEnd(String caption) {
		_out.print("\\caption{" + caption + "}" + "\n");
		_out.print("\\end{longtable}" + "\n");
		_out.print("}" + "\n");
		_out.print("\\clearpage" + "\n");
		_out.print("\\pagebreak" + "\n");
		_out.print("\n");
	}

	public void writeEnd() {
		_out.print("\\end{CJK*}" + "\n");
		_out.print("\\end{document}" + "\n");
	}

	public static String filterOutputString(String str){
		StringBuilder result = new StringBuilder();
		StringCharacterIterator iterator = new StringCharacterIterator(str);

		char character =  iterator.current();
		while (character != CharacterIterator.DONE ){
			if (character == '$') {
				result.append("\\$");
			} else if (character == '%') {
				result.append("\\%");
			} else {
				// the char is not a special one, add it to the result as is
				result.append(character);
			}

			character = iterator.next();
		}

		return result.toString();
	}

	public void close() {
		_out.close();
	}

	public PrintStream getPrintStream() {
		return _out;
	}
}
